<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/11
 * Time: 17:57
 */

namespace Biz\model\activity;


class PageModule extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'page_module';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    const MODULE_TYPE_IMG = 1;
    const MODULE_TYPE_GOODS = 2;
    const MODULE_TYPE_ANCHOR = 3;
    const MODULE_TYPE_TAB = 4;
    const MODULE_TYPE_RELATION = 5;
    const MODULE_TYPE_WIKI = 6;
    const MODULE_TYPE_CARD_BANNER = 7;
    const MODULE_TYPE_CARD_ARTICLE = 11;
    const MODULE_TYPE_FOLLOW = 8;
    const MODULE_TYPE_FLOAT = 9;
    const MODULE_TYPE_WATERFALL = 10;
    const MODULE_TYPE_MULTI_WATERFALL = 14;
    const MODULE_TYPE_VIDEO = 12;
    const MODULE_TYPE_CARD_WATERFALL = 13;
    const MODULE_TYPE_SPECIAL_GOODS = 15;
    const MODULE_TYPE_SPECIAL_ARTICLE = 16;
    const MODULE_TYPE_SPECIAL_NOTE = 17;
    const MODULE_TYPE_DESIGNER = 18;
    const MODULE_TYPE_GOODS_SET = 19;
    const MODULE_TYPE_BLIND_BOX = 20;
    const MODULE_TYPE_MAP = 21;
    const MODULE_TYPE_MULTI_FOLLOW = 22;
    const MODULE_TYPE_SHOP_BOOK = 24;

    const MODULE_TYPE_CPL = 23;

    const MODULE_STYLE_ONE = 1;
    const MODULE_STYLE_TWO = 2;
    const MODULE_STYLE_THREE = 3;
    const MODULE_STYLE_CAROUSEL = 11;

    const MODULE_TYPE_VIEW = [
        self::MODULE_TYPE_IMG             => "图片模块",
        self::MODULE_TYPE_GOODS           => "商品模块(节选)",
        self::MODULE_TYPE_ANCHOR          => "锚点导航条",
        self::MODULE_TYPE_TAB             => "tab导航条",
        self::MODULE_TYPE_WIKI            => "Wiki模块",
        self::MODULE_TYPE_CARD_BANNER     => "横滑卡片(banner)",
        self::MODULE_TYPE_CARD_ARTICLE    => "横滑卡片(文章)",
        self::MODULE_TYPE_FOLLOW          => "关注卡片",
        self::MODULE_TYPE_DESIGNER        => "设计师咨询卡片",
        self::MODULE_TYPE_SHOP_BOOK       => "门店预约模块",
        self::MODULE_TYPE_WATERFALL       => "话题瀑布流模块",
        self::MODULE_TYPE_CARD_WATERFALL  => "卡片瀑布流模块",
        self::MODULE_TYPE_FLOAT           => "悬浮模块",
        self::MODULE_TYPE_VIDEO           => "视频模块",
        self::MODULE_TYPE_SPECIAL_GOODS   => "商品模块(住友津贴)",
        self::MODULE_TYPE_SPECIAL_ARTICLE => "整屋卡片(榜单H5)",
        self::MODULE_TYPE_GOODS_SET => "好物合集",
        self::MODULE_TYPE_CPL=>'留资表单模块',
        self::MODULE_TYPE_SPECIAL_NOTE    => "note卡片(榜单H5)",
        self::MODULE_TYPE_BLIND_BOX       =>'超级盲盒',
        self::MODULE_TYPE_MAP       =>'活动地图',
        self::MODULE_TYPE_MULTI_FOLLOW       =>'全部关注按钮',

    ];

    const MODULE_SYLE_VIEW = [
        self::MODULE_STYLE_ONE      => "一行一个",
        self::MODULE_STYLE_TWO      => "一行两个",
        self::MODULE_STYLE_THREE    => "一行三个",
        self::MODULE_STYLE_CAROUSEL => "轮播"
    ];

    const DEFAULE_DATETIME = '0000-00-00 00:00:00';

    const MODULE_WATERFALL_SHOW_NUM = 1;
    const MODULE_WATERFALL_HIDE_NUM = 2;

    const MODULE_WATERFALL_TAB_DELIMITER = ':';

    /**
     * 获取类型常量名称
     * @param $moduleType
     * @param string $default
     * @return mixed|string
     */
    public static function getModuleTypeName($moduleType, $default = '')
    {
        return self::MODULE_TYPE_VIEW[$moduleType] ?? $default;
    }


    public static function saveModule($moduleInfo, $moduleId = 0)
    {
        if ($moduleId) {
            $moduleInfo['edittime'] = date("Y-m-d H:i:s");
            if (self::update($moduleInfo, ["id" => $moduleId])->rowcount()) {
                return $moduleId;
            } else {
                return false;
            }
        } else {
            $moduleInfo['status'] = self::STATUS_NORMAL;
            $moduleInfo['addtime'] = date("Y-m-d H:i:s");
            $moduleInfo['sort'] = PHP_INT_MAX;
            if (self::insert($moduleInfo)->rowcount()) {
                return self::id();
            } else {
                return false;
            }
        }
    }

    public static function getLastTabModuleIdByPageId($pageId)
    {
        return self::get("id", [
            'page_id' => $pageId,
            'tab[!]'  => '',
            'ORDER'   => [
                'end_time' => "DESC"
            ]
        ]);
    }

    public static function getModuleInfoByModuleId($moduleId)
    {
        $moduleInfo = self::get("*", ['id' => $moduleId]);
        if ($moduleInfo) {
            return self::formatModuleInfo($moduleInfo);
        } else {
            return [];
        }
    }

    public static function getModuleListByMoudleids($moduleIds)
    {
        $result = [];
        $moduleList = self::select("*", ['id' => $moduleIds]);
        if ($moduleList) {
            foreach ($moduleList as $moduleInfo){
                $result[] =  self::formatModuleInfo($moduleInfo);
            }
            return $result;
        } else {
            return [];
        }

    }

    private static function formatModuleInfo($moduleInfo)
    {
        if (isset($moduleInfo['ext']) && $moduleInfo['ext']) {
            $moduleInfo['ext'] = json_decode($moduleInfo['ext'], true);
        }
        return $moduleInfo;
    }

    public static function saveModuleSort($sortInfo)
    {
        foreach ($sortInfo as $moduleId => $sort) {
            self::update(['sort' => $sort], ['id' => $moduleId]);
        }
    }

    public static function getModuleListByPageId($pageId, $date = "")
    {
        $where = [
            'AND'   => [
                'page_id' => $pageId,
                'status'  => self::STATUS_NORMAL
            ],
            'ORDER' => [
                'sort',
                'id'
            ]
        ];
        if ($date) {
            $where['AND']['OR #startTime'] = [
                'start_time[<=]' => $date,
                'start_time'     => self::DEFAULE_DATETIME
            ];
            $where['AND']['OR #endTime'] = [
                'end_time[>=]' => $date,
                'end_time'     => self::DEFAULE_DATETIME
            ];
        }
        $rs = self::select("*", $where);
//        var_dump(self::log());
        return $rs;
    }

    public static function chageModuleStatusById($id, $status)
    {
        if (self::update(['status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }
}